package com.afterRain.admin.controller;

import com.afterRain.commonly.CommonlyRecord;
import com.afterRain.commonly.Prompt;
import com.afterRain.interceptor.UserInterceptor;
import com.afterRain.model.Admin;
import com.afterRain.util.MD5Util;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;

/**
* @author 作者 E-mail:
* @version 创建时间：2017年5月9日 下午2:28:17
* 类说明
*/
@Clear(UserInterceptor.class)
public class LoginController extends Controller{

	public void index(){
		Admin admin = getSessionAttr(CommonlyRecord.SESSION_USER.name());
		if(admin != null){
			redirect("/admin");
			return ;
		}
		render("login.ftl");
		setAttr("request", getRequest());
	}
	
	/**
	 * 登录验证
	 */
	@Before(POST.class)
	public void loginverify(){
		Admin admin = getBean(Admin.class, "");
		String sql = Admin.dao.getSql("selectByAdmin");
		admin = Admin.dao.findFirst(sql,admin.getAccount(),MD5Util.getMD5(admin.getPassword()));
		if(admin == null){
			renderJson(Prompt.NoLogin);
			return ;
		}
		setSessionAttr(CommonlyRecord.SESSION_USER.name(), admin);
		renderJson(Prompt.success);
	}
	
	public void logout(){
		removeSessionAttr(CommonlyRecord.SESSION_USER.name());
		redirect("/");
	}
}
